Tests: introduce base HighlighterTestCase, dedicated tests for the getCodeSnippet() + two bug fixes #35
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Tests: introduce base HighlighterTestCase
... from which all test classes should extend and which contains the
getConsoleColorMock()
method to prevent having to duplicate it in multiple places.The method has been adjusted to be slightly more flexible for re-use, in that the
$withTheme
parameter can be used to determine whether the ConsoleColor class should behave as if themes have been registered or not.Tests: add dedicated tests for the getCodeSnippet() method
These tests also test the
private
getHighlightedLines()
,splitToLines()
andlineNumbers()
methods and the data sets in the data provider have been set up to ensure those methods are tested thoroughly.Highlighter::getCodeSnippet(): bug fix - too many lines retrieved at start of file
When a code snippet in the middle of code is retrieved, the target snippet length calculation
$length = $linesAfter + $linesBefore + 1;
functions correctly, displaying the target line in the middle, padded by x number of lines before and after.Given
$linesBefore
and$linesAfter
both being set to two, this would display as:Similarly, when a code snippet at the end of the code is retrieved, it will work correctly as well, the target line is displayed with x number of lines before. The number of lines after is limited automatically by the length of the
$tokenLines
array.Given
$linesBefore
and$linesAfter
both being set to two, this would display as:However, when a code snippet at the start of the code is retrieved, the number of lines retrieved was incorrect. The target line would display with
($linesBefore + $linesAfter)
lines after the target line.Given
$linesBefore
and$linesAfter
both being set to two, this currently displays as:... while it should be:
The change in this commit fixes this bug.
Highlighter::lineNumbers(): bug fix - remove stray blank line at end of output
The
lineNumbers()
functionality adds a new line marker at the end of each line, including the last line, meaning that any code snippets being passed through this function will have a stray blank line at the end of the output.Fixed now.